package com.qeegoo.b2bautozityp.moudle.good.viewmodel;

import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.BR;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.good.model.GoodsPropertyBean;
import com.qeegoo.b2bautozityp.moudle.good.model.GoodsWareHouseBean;
import com.qeegoo.b2bautozityp.moudle.good.view.GoodsActivity;

import java.util.ArrayList;

import base.lib.util.ActivityManager;
import me.tatarka.bindingcollectionadapter.ItemView;

/**
 * 王少岩 在 2017/9/19 创建了它
 */

public class GoodsPropertyViewModel {
    public ObservableField<String> title = new ObservableField<>("");
    public ReplyCommand leftCommand = new ReplyCommand(() -> ((GoodsActivity) ActivityManager.getActivity()).closeGoodsPropertyFrag());

    public ItemView itemView = ItemView.of(BR.viewModel, R.layout.adapter_goods_property);
    public ObservableList<GoodsPropertyItemViewModel> items = new ObservableArrayList<>();

    private RequestApi mRequestApi;
    public GoodsPropertyViewModel(RequestApi requestApi) {
        mRequestApi = requestApi;
    }

    public void getWareHouse(String label, String info){
        mRequestApi.listWareHouses()
                .compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<ArrayList<GoodsWareHouseBean>>() {
                    @Override
                    public void onNext(ArrayList<GoodsWareHouseBean> beens) {
                        for (GoodsWareHouseBean bean:beens)
                            items.add(new GoodsPropertyItemViewModel(label, info, bean));
                    }
                });
    }

    public void setGoodsProperties(String label, String info, ArrayList<GoodsPropertyBean.GoodsProperty> goodsProperties){
        for (GoodsPropertyBean.GoodsProperty goodsProperty:goodsProperties)
            items.add(new GoodsPropertyItemViewModel(label, info, goodsProperty));
    }
}
